﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using B.S.BasicData.Infastructure.Migrations;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Text.RegularExpressions;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetUserHandler : IRequestHandler<GetUserCommand, ApiPaging<UserDto>>
    {
        private readonly IBaseRepository<UsersModel> _userRepository;
        private readonly IBaseRepository<UserRoleModel> _userRoleRepository;
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IBaseRepository<ProvinceModel> _proviceRepository;
        private readonly IBaseRepository<CityModel> _cityRepository;
        private readonly IBaseRepository<CountyModel> _countyRepository;
        private readonly IMapper _mapper;

        public GetUserHandler(IBaseRepository<UsersModel> userRepository, IBaseRepository<ProvinceModel> proviceRepository,
            IBaseRepository<CityModel> cityRepository,
            IBaseRepository<CountyModel> countyRepository, IMapper mapper, IBaseRepository<UserRoleModel> userRoleRepository, IBaseRepository<RoleModel> roleRepository)
        {
            _userRepository = userRepository;
            _proviceRepository = proviceRepository;
            _cityRepository = cityRepository;
            _countyRepository = countyRepository;
            _mapper = mapper;
            _userRoleRepository = userRoleRepository;
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<UserDto>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<UserDto>result = new ApiPaging<UserDto>();

            var user = _userRepository.GetAll().Where(c=>c.IsDelete==false);
            var provice=_proviceRepository.GetAll();
            var city = _cityRepository.GetAll();
            var county = _countyRepository.GetAll();

            

            var linq = (from u in user
                       join p in provice on u.ProvinceId equals p.Id 
                       join c in city on u.CityId equals c.Id 
                       join d in county on u.CountyId equals d.Id 
                       select new UserDto
                       {
                           Id = u.Id,
                           UserName = u.UserName,
                           UserSex = u.UserSex,
                           ProvinceId = p.Id,
                           ProvinceName = p.ProvinceName,
                           CityId = c.Id,
                           CityName = c.CityName,
                           CountyId = d.Id,
                           CountyName = d.CountyName,
                           AdderDetail = u.AdderDetail,
                           DeparmentId= u.DeparmentId,
                           CreateTime = u.CreateTime,
                           Creater = u.Creater,
                           Updater = u.Updater,
                           UpdateTime = u.UpdateTime,
                           RoleName=""
                       }).ToList();

            foreach( var item in linq )
            {
               var URlist=_userRoleRepository.GetAll().Where(p=>p.UserId==item.Id).AsNoTracking();
               var role = _roleRepository.GetAll().AsNoTracking();
               var query = from ur in URlist
                            join r in role on ur.RoleId equals r.Id
                            select new { ur, r };
                var s = query.Select(p => p.r.RoleName);
                item.RoleName = string.Join("、", s);
            }
            


            result.TotalCount=linq.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);
            result.PageData = linq.OrderByDescending(c => c.Id).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
            result.Message = "获取列表数据成功";
            return Task.FromResult(result); 
        }
    }
}
